package org.weishe.pagerank.hbase;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Counter;

import com.alibaba.fastjson.JSON;

public class PreMapper extends TableMapper<Text, Text> {

	private Text count = new Text("count");
	private Text one = new Text("1");
	private Text okey = new Text();
	private Text oValue = new Text();
	private String family = "cf1";
	private String qualifier = "obj";
	private double weight;// 权重

	@Override
	protected void map(ImmutableBytesWritable row, Result value, Context context)
			throws IOException, InterruptedException {
		// 用于计算count
		// context.write(count, one);
		// 运用计数器来返回当前总共行数
		final Counter counter = context.getCounter("PeopleRank", "sum");
		counter.increment(1l);
		String tempString = Text.decode(value.getValue(family.getBytes(), qualifier.getBytes()));
		PeopleRank r = JSON.parseObject(tempString, PeopleRank.class);
		String v = "" + r.getWight();
		// 输出到hdfs中
		if (r.getFans() != null && r.getFans().size() > 0) {
			ArrayList<String> list = r.getFans();
			for (String s : list) {
				v = v + "," + s;
			}
		}
		okey.set(r.getWho());
		oValue.set(v);
		context.write(okey, oValue);
	}
}